//
//  VRSafeKnowlegeListViewController.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/14.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRSafeKnowlegeListViewController.h"
#import "VRSafeKnowlegeDetailViewController.h"

#import "VRSafetyKnowledgeCell.h"

#import "VRSafetyKnowledgeModel.h"

@interface VRSafeKnowlegeListViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray<VRSafetyKnowledgeModel *> *safetyKnowledModelArray;
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation VRSafeKnowlegeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = ScrollViewBgColor;
    self.title = @"更多安全知识";
    
    self.safetyKnowledModelArray = [NSMutableArray array];
    self.currentPage = 1;
    [self setupSubView];
    
    weakify(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        strongify(self)
        [self getSafeKnowlegeWithIsRemoveArray:YES];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        strongify(self)
        [self getSafeKnowlegeWithIsRemoveArray:NO];
    }];

    [((MJRefreshAutoNormalFooter *)self.tableView.mj_footer) setTitle:@"没有更多了" forState:MJRefreshStateNoMoreData];
    
    [self getSafeKnowlegeWithIsRemoveArray:YES];
}

- (void)setupSubView
{
    
}

#pragma mark - networking
- (void)getSafeKnowlegeWithIsRemoveArray:(BOOL)isRemoveArray
{
    
    if (isRemoveArray) {
        self.currentPage = 1;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //    param[@"UserID"] = [VRUser shareUser].token;
    param[@"page"] = @(self.currentPage++);
    
    weakify(self)
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [JKNetworking postWithSign:GetAllSafeKnowlege parameters:param success:^(NSDictionary *responseObject) {
        
        strongify(self)
        NSLog(@"responseObject:%@", responseObject);
        NSInteger code = [responseObject[@"state"] integerValue];
        NSString *msg = responseObject[@"message"];
        
        if (code == 0) {
            
            NSArray *modelArray = [VRSafetyKnowledgeModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            if (isRemoveArray) {
                self.safetyKnowledModelArray = [NSMutableArray arrayWithArray:[modelArray copy]];
            }else{
                [self.safetyKnowledModelArray addObjectsFromArray:[modelArray copy]];
            }
            
            if (modelArray.count < 20) {
                
                [self.tableView.mj_footer setState:MJRefreshStateNoMoreData];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //                [self.tableView beginUpdates];
                //                [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
                [self.tableView.mj_header endRefreshing];
                [self.tableView reloadData];
                [hud hideAnimated:YES];
            });
        }else if(msg.length > 0){
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView.mj_header endRefreshing];
                [SVProgressHUD showErrorWithStatus:msg];
                [hud hideAnimated:YES];
            });
        }
    } error:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"未知错误"];
            [hud hideAnimated:YES];
            [self.tableView.mj_header endRefreshing];
        });
    }];
}


#pragma mark - delegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.safetyKnowledModelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 96 + 16 * 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VRSafetyKnowledgeCell *cell = [VRSafetyKnowledgeCell cellWith:tableView];
    cell.model = self.safetyKnowledModelArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    VRSafeKnowlegeDetailViewController *detailVc = [[VRSafeKnowlegeDetailViewController alloc] init];
    detailVc.fID = self.safetyKnowledModelArray[indexPath.row].fID;
    [self.navigationController pushViewController:detailVc animated:YES];
}

#pragma mark - lazy
- (UITableView *)tableView
{
    if (_tableView == nil) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = ScrollViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = [[UIView alloc] init];
        
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
        _tableView.tableFooterView = view1;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.sectionFooterHeight = 0.01;
        [self.view addSubview:self.tableView];
        //        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.right.bottom.mas_equalTo(self.view);
        //            make.top.mas_equalTo(0);
        //        }];
    }
    return _tableView;
}

@end
